// @商店功能
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const keys = require('../../config/keys');
const passport = require('passport');
const Shop = require('../../models/Shop/Shop');
const ShopGood = require('../../models/Shop/Shop_good');
const shop_api = require('../../controller/shop_api');
// @route  GET api/shops/get
// @desc   获取所有商店类型信息
// @access Private

router.get(
    '/get',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        Shop.find()
            .then(shop => {
                if (!shop) {
                    return res.status(404).json('没有任何内容');
                }
                res.json(shop);
            })
            .catch(err => res.status(404).json(err));
    }
);

// @route  GET api/shops/get/:id
// @desc   获取所有商店类型信息
// @access Private

router.get(
    '/get/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        Shop.findOne({ _id: req.params.id })
            .then(shop => {
                res.json(shop);
            })
            .catch(err => res.status(404).json('找不到对应id的商店'));
    }
);

// @route  Post api/shops/add
// @desc 新增商店
// @access Private
router.post(
    '/add',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        const Shop_Fields = {};
        if (req.body.name) Shop_Fields.name = req.body.name;
        if (req.body.info) Shop_Fields.info = req.body.info;
        new Shop(Shop_Fields).save().then(shop => {
            res.json(shop);
        });
    }
);

// @route  POST api/shops/edit/:id
// @desc   编辑商店信息接口
// @access Private
router.post(
    '/edit/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        const Shop_Fields = {};

        if (req.body.name) Shop_Fields.name = req.body.name;
        if (req.body.info) Shop_Fields.info = req.body.info;

        Shop.findOneAndUpdate(
            { _id: req.params.id },
            { $set: Shop_Fields },
            { new: true }
        ).then(shop => res.json(shop));
    }
);

// @route  POST api/shops/delete/:id
// @desc   删除商店接口
// @access Private
router.delete(
    '/delete/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        Shop.findOneAndRemove({ _id: req.params.id })
            .then(shop => {
                res.json(shop);
            })
            .catch(err => res.status(404).json('删除失败!'));
    }
);

// @route  GET api/shops/getitemlist/:id
// @desc   获取id商店所有商品信息
// @access Private

router.get(
    '/getitemlist/get/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        ShopGood.find({ shops: req.params.id })
            .populate(
                {
                    path: 'equipment',
                    populate: { path: 'equipments_type' }
                }
            )
            .populate('skill')
            .populate(
                {
                    path: 'goods',
                    populate: { path: 'goods_type' }
                }
            )
            .then(shopgoodlist => {
                if (!shopgoodlist) {
                    return res.status(404).json('找不到对应id的内容');
                }
                res.json(shopgoodlist);
            })
            .catch(err => res.status(404).json(err));
    }
);
// 商品相关功能

// @route  GET api/shops/goods/get
// @desc   获取所有商店商品信息
// @access Private

router.get(
    '/goods/get',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        ShopGood.find()
            .then(shopgood => {
                if (!shopgood) {
                    return res.status(404).json('没有任何内容');
                }
                res.json(shopgood);
            })
            .catch(err => res.status(404).json(err));
    }
);


// @route  Post api/shops/add
// @desc 新增商店
// @access Private

router.post(
    '/goods/add',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        const ShopGood_Fields = {};
        if (req.body.shops) ShopGood_Fields.shops = req.body.shops;
        if (req.body.goods) ShopGood_Fields.goods = req.body.goods;
        if (req.body.equipment) ShopGood_Fields.equipment = req.body.equipment;
        if (req.body.count) ShopGood_Fields.count = req.body.count;
        new ShopGood(ShopGood_Fields).save().then(shopgood => {
            res.json(shopgood);
        });
    }
);

// @route  get api/shops/get/:id
// @desc   获取商品信息接口
// @access Private
router.get(
    '/goods/get/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        ShopGood.find(
            { _id: req.params.id },
        ).then(shopgood => res.json(shopgood));
    }
);

// @route  POST api/shops/edit/:id
// @desc   编辑商品信息接口
// @access Private
router.post(
    '/goods/edit/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        const ShopGood_Fields = {};

        if (req.body.shops) ShopGood_Fields.shops = req.body.shops;
        if (req.body.goods) ShopGood_Fields.goods = req.body.goods;
        if (req.body.equipment) ShopGood_Fields.equipment = req.body.equipment;
        if (req.body.count) ShopGood_Fields.count = req.body.count;

        ShopGood.findOneAndUpdate(
            { _id: req.params.id },
            { $set: ShopGood_Fields },
            { new: true }
        ).then(shopgood => res.json(shopgood));
    }
);

// @route  POST api/shops/delete/:id
// @desc   删除商品接口
// @access Private
router.delete(
    '/goods/delete/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        ShopGood.findOneAndRemove({ _id: req.params.id })
            .then(shopgood => {
                res.json(shopgood);
            })
            .catch(err => res.status(404).json('删除失败!'));
    }
);

// @route  POST api/shops/buy/:id
// @desc   购买商品（物品）接口
// @access Private
router.post(
    '/goods/buy/:id',
    passport.authenticate('jwt', { session: false }),
    shop_api.buygood
);

module.exports = router;